package qdu.rg.mydraw;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

/**
 * 课堂练习
 * 1.实现移动和停止的控制
 *
 * 2. 取消求的运动，改为跟踪触屏移动
 *
 * 3. 保留运动轨迹（设置开关）
    4. 单击画布随机改变球的颜色
 */
public class Draw_act extends Activity {
    static boolean pathflag;
    //声明界面元素对应的成员
    LinearLayout ly_drawact;
    Button btn_move, btn_hold;
    //View v;
    Myview myv;
    Path mypath;
    //-----------------
    float radius = 50;
    float cx=0,cy=300;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.);
        initUI();
        pathflag = false;
        btn_move.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pathflag = true;
            }
        });
        btn_hold.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pathflag = false;
            }
        });
        //在非主线程（activity的UI线程）中，不允许访问UI元素
        //新的API允许简单的跨线程访问，提倡使用Handler
        new Thread() {
            public void run(){
                while(true){
//                    cx += 5;
//                    if(cx > myv.getWidth() + radius){
//                        cx = myv.getX() - radius;
//                    }
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    myv.invalidate();
                }

            }
        }.start();
    }

    private void initUI() {//初始化UI（contentView）
        //实例化各UI元素
        ly_drawact = new LinearLayout(this);
        btn_move = new Button(this); btn_hold = new Button(this);
        //v = new View(this);
        myv = new Myview(this);
        mypath = new Path();
        //处理布局
        btn_hold.setText("停止");
        btn_move.setText("移动");
        LinearLayout.LayoutParams llparas = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        btn_move.setLayoutParams(llparas);
        btn_hold.setLayoutParams(llparas);
        ly_drawact.addView(btn_move);
        ly_drawact.addView(btn_hold);
        //ly_drawact.addView(v);
        ly_drawact.addView(myv);
        ly_drawact.setOrientation(LinearLayout.VERTICAL);


        setContentView(ly_drawact);
    }

    class Myview extends View{

        public Myview(Context context) {
            super(context);
            this.setFocusable(true);
            this.setFocusableInTouchMode(true);
        }

        //重写View类中负责绘制的函数

        @Override
        protected void onDraw(Canvas c) {
            super.onDraw(c);
            c.drawColor(Color.CYAN);//绘制背景 大小有当前canvas所在的UI元素决定
            Paint p = new Paint();//Paint相当于画笔
            p.setColor(Color.RED);
            c.drawOval(cx-radius,cy-radius,cx+radius,cy+radius, p);
            if(pathflag){
                Paint ppath = new Paint();
                ppath.setColor(p.getColor());
                ppath.setStyle(Paint.Style.STROKE);
                ppath.setStrokeCap(Paint.Cap.ROUND);
                ppath.setStrokeWidth(radius);
                c.drawPath(mypath,ppath);
            }else{
                mypath.reset();
            }
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            int act = event.getAction();
            float x= event.getX(), y = event.getY();
            switch (act) {
                case MotionEvent.ACTION_DOWN:
                    mypath.moveTo(x,y);
                    break;
                case MotionEvent.ACTION_MOVE:
                    mypath.quadTo(cx,cy,x,y);
                    break;
                case MotionEvent.ACTION_UP:
                    //mypath.reset();
            }
            cx = x; cy = y;
            return true;//super.onTouchEvent(event);
        }
    }
}
